package HomeWork.SocketP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

public class Server {
    private ServerSocket serverSocket;
    public Server(){
        try {

                System.out.println("Ready to turn on the server..");
                serverSocket=new ServerSocket(8090);
                System.out.println("The server 8090 has been turned on");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start(){
        try {

            while (true) {
                System.out.println("Prepare to connect the client");
                Socket socket = serverSocket.accept();
                System.out.println("Connected!!");

                InputStream in=socket.getInputStream();
                InputStreamReader isr=new InputStreamReader(in);
                BufferedReader br=new BufferedReader(isr);
                String line;
                try {
                    while((line=br.readLine())!=null){
                        System.out.println(line);
                    }
                } catch (SocketException e) {
                    System.out.println("客户端非正常退出");
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server server=new Server();
        server.start();
    }
}
